<?php

/**
 * uniBoard API GeSHi GeSHi
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_geshi extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore				= NULL;
	
	/**
	 * GeSHi ID
	 *
	 * @var integer
	 */
	
	public $geshi_id 				= 0;
	
	/**
	 * Language Name
	 *
	 * @var string
	 */
	
	public $geshi_name				= NULL;
	
	/**
	 * Language Tag
	 *
	 * @var string
	 */
	
	public $geshi_tag				= NULL;
	
	/**
	 * Language File
	 *
	 * @var string
	 */
	
	public $geshi_file				= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store GeSHi ID
		$this -> geshi_id		= (integer) $target_id;
		
		// Autoload If GeSHi ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT *
			FROM ' . DB_PREFIX . 'bbcodes_geshi
			WHERE geshi_id = \'' . $this -> geshi_id . '\'
			LIMIT 1');
			
			// GeSHi found
			if ( $geshi_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded				= true;
				
				$this -> geshi_name	 				= $geshi_result['geshi_name'];
				$this -> geshi_tag		 			= $geshi_result['geshi_tag'];
				$this -> geshi_file	 				= $geshi_result['geshi_file'];
			}
			else
			{
				//give action an error
				$this -> setError( $appCore -> lang['acp_geshi_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> geshi_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'bbcodes_geshi');
			
				// Store new GeSHi id
				$this -> geshi_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'bbcodes_geshi', 'geshi_id = \'' . $this -> geshi_id . '\'');
			}
			
			// Flush Cache
			class_cache::flushCache( 'bbcodes_geshi');
			class_cache::flushCache( 'parsing_data');
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Delete API data to DB
	 *
	 */
	
	function deleteData(){
		
		// Delete data
		$this -> appCore -> db -> delete( 'bbcodes_geshi', 'geshi_id = \'' . $this -> geshi_id . '\'');
		
		// Flush Cache
		class_cache::flushCache( 'bbcodes_geshi');
		class_cache::flushCache( 'parsing_data');
	
	}
		
	// ------------------------------------------------------------------------
	
	/**
	 * Set geshi_name
	 *
	 * @param string $new_value
	 */
	
	function setGeSHiName( $new_value){
		
		// Not empty?
		if ( isset( $new_value[0]))
		{
			// Store in Api
			$this -> geshi_name = $new_value;
			
			// Too long?
			if ( mb_strlen( $new_value) > 255)
			{
				// Set Error
				$this -> setError( $this -> appCore -> lang['acp_geshi_name_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['geshi_name'] = $this -> appCore -> db -> strEscape( $this -> geshi_name);
			}
		}
		else
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_geshi_name_empty']);
		}
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set geshi_tag
	 *
	 * @param string $new_value
	 */
	
	function setGeSHiTag( $new_value){
		
		// Not empty?
		if ( isset( $new_value[0]))
		{
			// Store in Api
			$this -> geshi_tag = $new_value;
			
			// Too long?
			if ( mb_strlen( $new_value) > 255)
			{
				// Set Error
				$this -> setError( $this -> appCore -> lang['acp_geshi_tag_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['geshi_tag'] = $this -> appCore -> db -> strEscape( $this -> geshi_tag);
			}
		}
		else
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_geshi_tag_empty']);
		}
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set geshi_file
	 *
	 * @param string $new_value
	 */
	
	function setGeSHiFile( $new_value){
		
		// Not empty?
		if ( isset( $new_value[0]))
		{
			// Store in Api
			$this -> geshi_file = str_replace( array('../', '/', '..\\', '\\'), '', $new_value);
			
			// File exsits?
			if ( file_exists( GESHI_DATA_PATH . '/' . $this -> geshi_file . PHP_EXT))
			{
				// Add to made changes
				$this -> changes['geshi_file'] = $this -> appCore -> db -> strEscape( $this -> geshi_file);
			}
			else
			{
				// Set Error
				$this -> setError( $this -> appCore -> lang['acp_geshi_file_incorrect']);
			}
		}
		else
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_geshi_tag_empty']);
		}
	
	}
	
}